package com.pmsystem.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pmsystem.dao.RoomMapper;
import com.pmsystem.dao.VillageMapper;
import com.pmsystem.pojo.Building;
import com.pmsystem.pojo.BuildingShow;
import com.pmsystem.pojo.Room;
import com.pmsystem.pojo.RoomExample;
import com.pmsystem.pojo.RoomExample.Criteria;
import com.pmsystem.pojo.RoomShow;
import com.pmsystem.pojo.User;
import com.pmsystem.pojo.Village;

/**
 * 
* <p>Title: RoomService</p>  
* <p>Description:处理房间信息的业务层 </p>  
* @author 树昌  
* @date 2019年1月22日
 */
@Service
public class RoomService {

	@Autowired
	RoomMapper roomMapper;
	@Autowired
	BuildingService buildingService;
	@Autowired
	UserService userService;
	
	
	/**
	 * 
	 * <p>Title: addRoom</p>  
	 * <p>Description:添加一条房间记录 </p>  
	 * @author 树昌
	 * @param room
	 */
	public boolean addRoom(Room room) {
		 int row = roomMapper.insertSelective(room);
		 return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: showRoom</p>  
	 * <p>Description:显示所有的房间记录,包括模糊查询和分页查询功能，没有则为NULL</p>  
	 * @author 树昌
	 * @return 
	 */
	public List<RoomShow> showRoom(String keyword, Integer startRow, Integer pageSize,Integer RID){
		
		RoomExample roomExample = new RoomExample();
		Criteria criteria = roomExample.createCriteria();
		criteria.andEnableEqualTo((byte) 1);
		//条件查询
		if(RID!=null) {
			criteria.andRoomIdEqualTo(RID);
		}
		//模糊查询
		if(keyword!=null) {
			keyword="%"+keyword+"%";
			criteria.andRoomNameLike(keyword);
		}
		//分页查询
		if (startRow != null && pageSize != null) {
			if (((startRow-1)*pageSize) >= 0 && pageSize >= 1) {
				roomExample.setStartRow((startRow-1)*pageSize);
				roomExample.setPageSize(pageSize);
			} else {
				return null;
			}
		}
		//没有条件则为全查询
		List<Room> listroom =roomMapper.selectByExample(roomExample);
		List<BuildingShow> listbuilding = buildingService.showBuilding(null, null, null,null);
		List<RoomShow> listroomshow = new ArrayList<>();
		List<User> listuser = userService.showUser();
		
		
		for (Room room : listroom) {
			System.out.println(room.getRoomId()+"---"
								+room.getRoomName()+"---"
								+room.getFloor()+"---"
								+room.getBlgBuildingId()+"---"
								+room.getBlgUserId());
			
			RoomShow roomShow = new RoomShow();
			roomShow.setRoomId(room.getRoomId());
			roomShow.setRoomName(room.getRoomName());
			roomShow.setFloor(room.getFloor());
			for (BuildingShow building : listbuilding) {
				
				if(room.getBlgBuildingId()==building.getBuildingId()) {
					roomShow.setBuildingName(building.getBuildingName());
					roomShow.setVillageName(building.getVillageName());
				}
			}
			if(room.getRoomId()!=null) {
				for (User user : listuser) {
					if(room.getBlgUserId()==user.getUserId())
					roomShow.setUserName(user.getPresonName());
				}
			}
			
			listroomshow.add(roomShow);
		}
		return listroomshow;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteRoom</p>  
	 * <p>Description:删除一条房间的记录 </p>  
	 * @author 树昌
	 * @param room
	 */
	public boolean deleteRoom(Room room) {
		
		RoomExample roomExample = new RoomExample();
		Criteria criteria = roomExample.createCriteria();
		criteria.andRoomIdEqualTo(room.getRoomId());
		room.setEnable((byte) 0);
		int row = roomMapper.updateByExampleSelective(room, roomExample);
		return row==1?true:false;
	}
	
	
	/**
	 * 
	 * <p>Title: updateRoom</p>  
	 * <p>Description:修改一条房间的记录 </p>  
	 * @author 树昌
	 * @param room
	 */
	public boolean updateRoom(Room room) {
		RoomExample roomExample =new RoomExample();
		Criteria criteria =roomExample.createCriteria();
		criteria.andRoomIdEqualTo(room.getRoomId());
		int row = roomMapper.updateByExampleSelective(room, roomExample);
		return row==1?true:false;
	}
	
	
	public boolean deleteRoomTableData() {
		
		RoomExample roomExample =new RoomExample();
		int row = roomMapper.deleteByExample(roomExample);
		return row==1?true:false;
	}
}
